<template>
  <link rel="stylesheet" href="/assets/css/profile.css" />

  <van-sticky>
    <van-nav-bar title="邮箱验证" left-text="返回" left-arrow @click-left="onClickLeft" />
  </van-sticky>

  <div class="user">
    <div class="user-image">
      <img :src="business.avatar_cdn" alt="" />
    </div>
  </div>

  <van-form @submit="onSubmit">
    <van-cell-group inset>
      <van-field v-model="business.email" name="email" label="邮箱"
        disabled="true" />
      <van-field v-model="reset" type="text" name="密码" label="验证码" placeholder="请输入验证码" :disabled="disabled"
        :rules="[{ required: true, message: '请填写验证码' }]" />
    </van-cell-group>
    <div style="margin: 16px">
      <van-button round block type="primary" native-type="submit" @click="send" :disabled="disabled">
        发送
      </van-button>
      <van-button round block type="primary" native-type="submit" style="margin-top: 16px" :disabled="disabled">
        确认
      </van-button>
    </div>
  </van-form>
</template>

<script setup>
const { proxy } = getCurrentInstance();

const business = ref(proxy.$business);
const reset = ref("");
const disabled = ref(false);

onBeforeMount(() => {
  yanzheng();
});

//判断是否以及验证
const yanzheng = async () => {
  let data = {
    busid: business.value.id,
    auth: 1
  }

  let result = await proxy.$post({
    url: "business/send/check",
    params: data
  });

  if (result.code === 1) {
    proxy.$NotifySuccess("已验证成功");
    reset.value = "验证成功";
    disabled.value = true;
  } else {
    proxy.$NotifyWarn("需要验证");
  }
}
//验证邮箱
const onSubmit = async () => {
  let data = {
    reset: reset.value,
    busid: business.value.id,
    email: business.value.email
  }

  let result = await proxy.$post({
    url: "business/send/email",
    params: data
  });

  if (result.code === 1) {
    proxy.$NotifySuccess("验证成功");
  } else {
    proxy.$NotifyWarn("验证失败");
  }
};

//发送验证码
const send = async () => {
  let data = {
    busid: business.value.id,
    email: business.value.email
  }
  let result = await proxy.$post({
    url: "business/send/send",
    params: data
  });

  if (result.code === 1) {
    proxy.$NotifySuccess("发送成功");
  } else {
    proxy.$NotifyWarn("发送失败");
  }

};

//返回上一级
const onClickLeft = () => {
  proxy.$router.back();
};
</script>